package com.cino.controller;

import com.cino.domain.Teacher;
import com.cino.service.ITeacherService;
import com.cino.util.AjaxResult;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cino.util.AjaxResult.*;

/**
 * 老师控制器
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    /**
     *获取所有老师
     * @return
     */
    @GetMapping
    public AjaxResult<Teacher> getAllTeacher(){
        try {
            List<Teacher> all = teacherService.getAll();
            return  AjaxResult.success(all);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.failed(e.getMessage());
        }
    }

    /**
     *查询单个老师对象
     * @param id
     * @return
     */
    @GetMapping("id")
    public AjaxResult<Teacher> getOneTeacher(@RequestParam Long id){
        try {
            return AjaxResult.success( teacherService.getOne(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.failed(e.getMessage());
        }
    }

    /**
     * 添加老师
     * @param teacher
     * @return
     */
    @PutMapping("/save")
    public AjaxResult<Teacher> saveTeacher(@RequestBody Teacher teacher){
        try {
            teacherService.save(teacher);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.failed(e.getMessage());
        }
    }

    /**
     *更新老师对象
     * @param teacher
     * @return
     */
    @PostMapping("/update")
    public AjaxResult<Teacher> updateTeacher(@RequestBody Teacher teacher){
        try {
            teacherService.update(teacher);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.failed(e.getMessage());
        }
    }

    /**
     *删除单个老师
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult<Teacher> deleteTeacher(@PathVariable Long id){
        try {
            teacherService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.failed(e.getMessage());
        }
    }



}
